package com.hospital.doctor.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.constants.TokenConstants;
import com.hospital.common.domain.Admin;
import com.hospital.common.utils.JwtUtils;
import com.hospital.doctor.domain.ConsultRecordInfo;
import com.hospital.doctor.domain.vo.ConsultRecordVo;
import com.hospital.doctor.mapper.EltConsultRecordMapper;
import com.hospital.doctor.service.EltConsultRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author：zhuzhujiang
 * @Description:
 * @name：EltConsultRecordServiceImpl
 * @Date：2024/10/17 20:58
 */
@Service
public class EltConsultRecordServiceImpl extends ServiceImpl<EltConsultRecordMapper, ConsultRecordInfo> implements EltConsultRecordService {

    @Autowired
    private EltConsultRecordMapper eltConsultRecordMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;

    @Override
    public List<ConsultRecordVo> getConsultRecord(Long departmentId) {

        //获取当前登录医生
        String token = request.getHeader("token");
        String userKey = JwtUtils.getUserKey(token);
        String userStr = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        Admin doctor = JSONObject.parseObject(userStr, Admin.class);
        List<ConsultRecordVo> consultRecord = eltConsultRecordMapper.getConsultRecord(doctor.getId(),departmentId);

        return consultRecord;
    }

    @Override
    public void finishConsult(ConsultRecordInfo consultRecordInfo) {

        System.out.println(JSON.toJSONString(consultRecordInfo));
        boolean b = this.updateById(consultRecordInfo);
        if (!b){
            throw new RuntimeException("结束问诊失败");
        }


    }
}
